package com.woniuxy.controller;


import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import com.woniuxy.dto.ResponseResult;

import com.woniuxy.enums.ResponseStatues;
import com.woniuxy.util.SetResponseResult;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author: ideaAdmin
 * TODO:
 * Date: 2022/2/7:18:52
 * Description:
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;


    // 登录方法
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    private ResponseResult login(@RequestBody User user, HttpServletRequest request) {
        System.out.println(user);
        ResponseResult responseResult;

        // 调用Service查询数据
        try {
            user = userService.findByAccount(user.getAccount(), user.getPassword());
            responseResult = SetResponseResult.setResponseResult(200, ResponseStatues.LOGIN_SUCCESS, "登陆成功", null);

            request.getSession().setAttribute("user",user.getAccount());
        } catch (RuntimeException e) {
            responseResult = SetResponseResult.setResponseResult(500, ResponseStatues.LOGIN_FAIL, "登陆失败", null);
            e.printStackTrace();
        }
        return responseResult;
    }

    // 注册
    @ResponseBody
    @RequestMapping(value = "/register")
    private ResponseResult register(User user) {
        System.out.println(user);

        ResponseResult responseResult;

        if (userService.findAccount(user.getAccount()) != null) {

            responseResult = SetResponseResult.setResponseResult(500, ResponseStatues.ERROR,
                    "账号已存在", null);
            return responseResult;
        }


        if (userService.addUser(user)) {
            // 注册成功
            responseResult = SetResponseResult.setResponseResult(200, ResponseStatues.SUCCESS,
                    "注册成功,激活邮件已发送", null);
        } else {
            // 注册失败

            responseResult = SetResponseResult.setResponseResult(500, ResponseStatues.ERROR,
                    "注册失败,请稍后再试", null);
        }
        return responseResult;
    }

    @ResponseBody
    @RequestMapping(value = "/findEmail")
    // 判断邮箱是否存在 时时发送数据
    private ResponseResult findEmail(String email) {
        ResponseResult responseResult;


        if (userService.findByEmail(email) != null) {

            responseResult = SetResponseResult.setResponseResult(500, ResponseStatues.ERROR,
                    "邮箱已存在", null);

        } else {

            responseResult = SetResponseResult.setResponseResult(200, ResponseStatues.SUCCESS,
                    "邮箱不存在", null);
        }

        return responseResult;

    }

    // 判断用户是否存在 时时发送数据
    @ResponseBody
    @RequestMapping("/findAccount")
    private ResponseResult findAccount(String username) {

        ResponseResult responseResult;


        if (userService.findAccount(username) != null) {

            responseResult = SetResponseResult.setResponseResult(500, ResponseStatues.SUCCESS,
                    "账号已存在", null);

        } else {

            responseResult = SetResponseResult.setResponseResult(200, ResponseStatues.SUCCESS,
                    "账号不存在", null);
        }
        return responseResult;
    }


}
